package gazzera.utils;

import java.io.*;
import java.math.*;

import com.sleepycat.persist.model.*;

@Persistent
public class Hash implements Comparable<Hash>, Serializable, FileID
{
	private static final long serialVersionUID = -2414557080056753414L;

	public static final Hash EMPTY = new Hash();
	
	@KeyField(1)
	private BigInteger x; 
	
	public Hash(byte[] hash)
	{
		super();
		x = new BigInteger(1, hash);
	}

	public Hash(String h)
	{
		x = new BigInteger(h, 16);
	}
	public Hash()
	{
		super();
		x = BigInteger.ZERO;
	}
	

	@Override
	public int hashCode()
	{
		return x.hashCode();
	}

	public Hash(BigInteger b)
	{
		super();
		x = b;
	}

	public String toString()
	{
		return x.toString(16);
	}

	@Override
	public int compareTo(Hash o)
	{
		return x.compareTo(o.x);
	}

	public BigInteger toBigInteger()
	{
		return x;
	}

	public byte[] toByteArray()
	{
		return x.toByteArray();
	}
	

	@Override
	public boolean equals(Object obj)
	{
		if(obj instanceof Hash)
		{
			return x.equals(((Hash)obj).x);
		}
		return false;
	}

	
	
	
	
}
